﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Windows.Forms;

namespace 多行记录合并
{
    public partial class ThisAddIn
    {

        const string m_MergeMenuTag = "Merge_BC81D1AA-B954-4B0C-A60C-07780C03CDEB";

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            AddRightMenu();
            this.Application.WorkbookOpen +=  new Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);
        }

        void Application_WorkbookOpen(Excel.Workbook Wb)
        {
            Wb.SheetBeforeRightClick += new Excel.WorkbookEvents_SheetBeforeRightClickEventHandler(ThisWorkbook_SheetBeforeRightClick);
        }

        void ThisWorkbook_SheetBeforeRightClick(object Sh, Excel.Range Target, ref bool Cancel)
        {
            bool bWithHead = Globals.Ribbons.Ribbon1.IsHaveHead();
            bool bIsMenuEnabled = MergeText.IsSelectionValid(bWithHead);
            UpdateMenuState(m_MergeMenuTag,bIsMenuEnabled );
        }


        private void MergeTextHandler(Office.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            bool bWithHead = Globals.Ribbons.Ribbon1.IsHaveHead();
            MergeText.DoMergeText(bWithHead);
        }

        public  void AddRightMenu()
        {
            Microsoft.Office.Core.CommandBar mzBar = this.Application.CommandBars["cell"];    //excel文档已有的右键菜单cell                

            Microsoft.Office.Core.CommandBarControls bars = mzBar.Controls;

            foreach (Microsoft.Office.Core.CommandBarControl temp_contrl in bars)
            {
                //如果已经存在就删除
                if (temp_contrl.Tag == m_MergeMenuTag)
                {
                    temp_contrl.Delete();
                }
            }


            Microsoft.Office.Core.CommandBarControl comControl = bars.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, missing, missing, missing, true);   //添加自己的菜单项
            Microsoft.Office.Core.CommandBarButton comButton = comControl as Microsoft.Office.Core.CommandBarButton;
            if (comControl != null)
            {
                comButton.Tag = m_MergeMenuTag;
                comButton.Caption = "合并多行文本";
                comButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
                comButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(MergeTextHandler);
            }
        }

        public void UpdateMenuState(string tag, bool bEnabled)
        {
            Microsoft.Office.Core.CommandBar mzBar = this.Application.CommandBars["cell"];    //excel文档已有的右键菜单cell                

            Microsoft.Office.Core.CommandBarControls bars = mzBar.Controls;

            foreach (Microsoft.Office.Core.CommandBarControl temp_contrl in bars)
            {
                if (temp_contrl.Tag == m_MergeMenuTag)
                {
                    temp_contrl.Enabled = bEnabled;
                }
            }

        }
        
        

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO 生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
